package ch.ethz.fcl.metrobuzz.render.techniques.bezier;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

import ch.ethz.fcl.metrobuzz.MBContainer;
import ch.ethz.fcl.metrobuzz.controller.MBController;
import ch.ethz.fcl.metrobuzz.spatialindex.object2d.Point2D;
import ch.ethz.fcl.metrobuzz.tools.MBGL;
import ch.ethz.fcl.metrobuzz.viewer.MBViewer;

public class BezierTest {
	float[][] ctrlCurvePoints = { { -0.5f, 0.5f }, { 0, 0.5f }, { 0.5f, 0.2f },
			{ 0.8f, -0.1f } };

	Point2D[] ctrlPoints;
	Point2D[] spline;

	private MBViewer view;
	private MBController control;

	public BezierTest() {
		init();
	}

	public void run() {
		while (true) {

			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			GL11.glPushMatrix();
			view.update();
			render();
			control.update();
			GL11.glPopMatrix();

			Display.update();
		}
	}

	private void init() {
		MBContainer container = new MBContainer();

		int width = 800;
		int height = 600;

		view = new MBViewer(MBViewer.ProjectionType.Perspective, container);
		view.createDisplay(width, height, width, height, "Bezier Test");
		view.getCamera().disablePan();
		view.getCamera().initDistance(2.5f);

		control = new MBController(container);

		ctrlPoints = new Point2D[ctrlCurvePoints.length];
		for (int i = 0; i < ctrlCurvePoints.length; i++)
			ctrlPoints[i] = new Point2D(ctrlCurvePoints[i][0],
					ctrlCurvePoints[i][1]);

		spline = Bezier.getContinuousBezierSpline(Math.PI / 4, ctrlPoints);
	}

	private void render() {
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
		GL11.glClearColor(1, 1, 1, 1);

		GL11.glPushMatrix();

		MBGL.enableBlend();
		GL11.glLineWidth(2.0f);
		GL11.glColor3f(0, 0, 1);
		MBGL.render(spline, GL11.GL_LINE_STRIP);

		GL11.glPointSize(5.0f);
		GL11.glColor3f(1, 0, 0);
		MBGL.render(ctrlPoints, GL11.GL_POINTS);

		GL11.glPopMatrix();
	}

	public static void main(String[] arguments) {
		System.out.println("In Bezier Test");
		new BezierTest().run();
	}
}
